package com.chenshu.shop.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-04
 * Time：18:04
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private TypeListInterceptor typeListInterceptor;
    @Autowired
    private CartInterceptor cartInterceptor;

    List<String> excludes = new ArrayList<String>() {{
        //放行登录和注册的接口
        add("/index/login");
        add("/index/reg");
        add("/admin/login");
        add("/**/*.jsp");
        add("/**/*.css");
        add("/**/*.js");
        add("/index/search");
        add("/**/*.png");
    }};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登陆拦截器
        InterceptorRegistration registration =
                registry.addInterceptor(loginInterceptor);
        registration.addPathPatterns("/**");
        registration.excludePathPatterns(excludes);

        //查找typeList的拦截器
        InterceptorRegistration registration2 =
                registry.addInterceptor(typeListInterceptor);
        registration2.addPathPatterns("/index/**");

        InterceptorRegistration registration3 =
                registry.addInterceptor(cartInterceptor);
        registration3.addPathPatterns("/index/**");
    }
}
